package com.yuncheng.spcyApi.spcyService;

import cn.hutool.core.collection.CollectionUtil;
import com.yuncheng.spcyApi.constant.common.SpcyConstant;
import com.yuncheng.spcyApi.constant.xcjchy.SqsxHyConstant;
import com.yuncheng.spcyApi.dto.SqsxShjPrintDataDto;
import com.yuncheng.spcyApi.entity.*;
import com.yuncheng.spcyApi.flow.SpcyFlowConstant;
import com.yuncheng.spcyApi.mapper.SpcySqsxHyztMapper;
import com.yuncheng.spcyApi.mapper.SpcySqsxJobMapper;
import com.yuncheng.spcyApi.mapper.YcSysUserMapper;
import com.yuncheng.spcyApi.vo.common.HttpResultVo;
import com.yuncheng.spcyApi.vo.spcy.SqsxShjPrintDataVo;
import com.yuncheng.spcyApi.vo.spcy.ZhpdshjPrintDataVo;
import com.yuncheng.spcyApi.vo.sys.UserRealnameVo;
import org.apache.commons.collections.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.zxing.WriterException;
import com.yuncheng.spcyApi.busService.QRCodeService;
import com.yuncheng.spcyApi.config.SpcyMinioUtils;
import com.yuncheng.spcyApi.constant.common.SpcyCommonConstant;
import com.yuncheng.spcyApi.constant.wjml.WjmlConstant;
import com.yuncheng.exception.BusinessException;
import com.yuncheng.spcyApi.service.ISpcySqsxBaiscService;
import com.yuncheng.spcyApi.service.ISpcySqsxWjjlService;
import com.yuncheng.spcyApi.service.ISpcySqsxWjmlService;
import com.yuncheng.spcyApi.utils.GetCurrentUserUtils;
import com.yuncheng.spcyApi.utils.GetUuIdUtils;
import com.yuncheng.spcyApi.utils.LocalFileUploadUtils;
import com.yuncheng.spcyApi.utils.StringPatternUtils;
import com.yuncheng.vo.HttpResult;
import com.yuncheng.spcyApi.vo.hbjc.HbsxidVo;
import com.yuncheng.spcyApi.vo.wj.WjjlVo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import utils.DateConversionUtils;
import utils.PdfUtils;
import utils.Util;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 审评查验-办理过程材料
 */
@Component(value = "spcyBlgcclService")
public class SpcyBlgcclService {
    private static final Logger log = LoggerFactory.getLogger(SpcyBlgcclService.class);

    @Resource
    @Lazy
    private ISpcySqsxWjjlService spcySqsxWjjlService;

    @Resource
    @Lazy
    private ISpcySqsxWjmlService spcySqsxWjmlService;

    @Resource
    @Lazy
    private ISpcySqsxBaiscService spcySqsxBaiscService;

    @Resource
    private SpcyMinioUtils spcyMinioUtils;

    @Resource
    private LocalFileUploadUtils localFileUploadUtils;

    @Resource
    private SqsxYwxxInfoService sqsxYwxxInfoService;

    @Resource
    private SpcySqsxJobMapper spcySqsxJobMapper;

    @Resource
    private YcSysUserMapper ycSysUserMapper;

    @Resource
    private SpcyMyInfoService spcyMyInfoService;

    @Resource
    @Lazy
    private SpcySqsxHyztMapper spcySqsxHyztMapper;

    /**
     * 获取办理过程材料-所有顶层目录结构
     * @return{
     *      [{
     *          "mcList" : [
     *              {
     *                  "clmc": "材料类型",
     *                  "wjList": "对应材料类型的文件记录"
     *              }
     *          ],
     *          "mlmc": "目录名称",
     *          "mlSize": "目录文件数量"
     *      }]
     * }
     * @param sqsxid
     */
    public List<Map> queryBlgcclMlList(String sqsxid){
        if (StringUtils.isBlank(sqsxid)){
           throw new BusinessException("申请事项参数为空！");
        }
        String fywflcode = spcySqsxBaiscService.getYwflCodeBySqsxId(sqsxid);

        List<Map> resultMapList = new ArrayList<>();

        WjjlVo wjjlVo = new WjjlVo();
        wjjlVo.setFywflcode(fywflcode);
        List<WjjlVo> wjjlList = spcySqsxWjjlService.findWjjlList(wjjlVo);

        // 当前申请事项-业务的文件目录
        List<SpcySqsxWjml> wjmlList = spcySqsxWjmlService.list(new LambdaQueryWrapper<SpcySqsxWjml>()
                .eq(StringUtils.isNotBlank(fywflcode), SpcySqsxWjml::getFywflcode, fywflcode)
                .orderByAsc(SpcySqsxWjml::getForder));

        // 获取所有目录
        List<String> clmlList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(wjmlList)){
            clmlList = wjmlList.stream()
                    .map(f -> f.getFwjlx1())
                    .filter(f->StringUtils.isNotBlank(f))
                    .distinct()
                    .collect(Collectors.toList());
        }

        if (CollectionUtils.isNotEmpty(clmlList)){
            Map clmlMap = null;
            Map wjjlMap = null;
            List<Map> wjjlMapList = null;
            List<WjjlVo> wjjlVoList = new ArrayList<>();

            for (String clml : clmlList){
                clmlMap = new HashMap();
                wjjlMapList = new ArrayList<>();
                int mlSize = 0; // 目录文件数量

                clmlMap.put("mlmc", clml); // 目录

                // 获取目录下的所以类型
                List<SpcySqsxWjml> lxmcList = new ArrayList<>();
                if (CollectionUtils.isNotEmpty(wjmlList)){
                    lxmcList = wjmlList.stream()
                            .filter(f -> StringUtils.isNotBlank(f.getFwjlx1())
                                    && f.getFwjlx1().equals(clml))
                            .collect(Collectors.toList());
                }

                // 将目录下的所有文件类型-去重
                List<String> lxmcMlList = new ArrayList<>();
                if (CollectionUtils.isNotEmpty(lxmcList)){
                    lxmcMlList = lxmcList.stream()
                            .map(f -> f.getFlxmc())
                            .distinct()
                            .collect(Collectors.toList());
                }

                // 对应目录下的类型文件
                if (CollectionUtils.isNotEmpty(lxmcMlList)){
                    for (String lxmc : lxmcMlList){
                        wjjlMap = new HashMap();

                        // 从所有的文件记录从过滤出来 对应的类型文件
                        if (CollectionUtils.isNotEmpty(wjjlList)) {
                            wjjlVoList = wjjlList.stream()
                                    .filter(f -> StringUtils.isNotBlank(f.getFwjlx1())
                                            && f.getFwjlx1().equals(lxmc))
                                    .collect(Collectors.toList());
                        }else {
                            wjjlVoList = new ArrayList<>(1);
                        }

                        wjjlMap.put("clmc", lxmc); // 目录对应类型
                        wjjlMap.put("wjList", wjjlVoList); // 目录对应类型文件

                        mlSize += wjjlVoList.size(); // 文件大小

                        wjjlMapList.add(wjjlMap);
                    }
                }

                clmlMap.put("mcList", wjjlMapList); // 目录-对应文件list
                clmlMap.put("mlSize", mlSize); // 目录-文件Size

                resultMapList.add(clmlMap);
            }
        }

        return resultMapList;
    }

    /**
     * 获取现场检查文件目录 (现场检查材料)
     * @param sqsxid 申请事项id
     * @param selectMl 指定查询目录名称 （现场检查材料）
     * @param isSelectPdf true: 显示文档以为的文件
     */
    public List<Map> queryXcjcFileMl(String sqsxid, String selectMl, boolean isSelectPdf){

//        String fywflcode = spcySqsxBaiscService.getYwflCodeBySqsxId(sqsxid);
        HbsxidVo hbsxidVo = spcySqsxBaiscService.selectHbsxidAndCodeById(sqsxid);
        if (hbsxidVo == null){
            return null;
        }

        String fywflcode = hbsxidVo.getFywflcode();
        List<Map> reusltMapList = new ArrayList<>();

        // 获取申请事项文件记录
        WjjlVo wjjlVo = new WjjlVo();
        wjjlVo.setFpid(sqsxid);
        wjjlVo.setFywflcode(fywflcode);
        List<WjjlVo> wjjlList = spcySqsxWjjlService.findWjjlList(wjjlVo);

        // 当前申请事项-业务的文件目录
        /*List<SpcySqsxWjml> wjmlList = spcySqsxWjmlService.list(new LambdaQueryWrapper<SpcySqsxWjml>()
                .eq(StringUtils.isNotBlank(fywflcode), SpcySqsxWjml::getFywflcode, fywflcode)
                .eq(StringUtils.isNotBlank(selectMl), SpcySqsxWjml::getFwjlx1, selectMl)
                .orderByAsc(SpcySqsxWjml::getForder));*/

        /*if (CollectionUtils.isNotEmpty(wjmlList)){
            clmlList = wjmlList.stream().map(f -> f.getFwjlx1()).distinct().collect(Collectors.toList());
        }*/
        List<String> clmlList = null;
        if (StringUtils.isBlank(selectMl)){
            List<SpcySqsxWjml> wjmlList = spcySqsxWjmlService.list(new LambdaQueryWrapper<SpcySqsxWjml>()
                    .eq(StringUtils.isNotBlank(fywflcode), SpcySqsxWjml::getFywflcode, fywflcode)
                    .eq(StringUtils.isNotBlank(selectMl), SpcySqsxWjml::getFwjlx1, selectMl)
                    .orderByAsc(SpcySqsxWjml::getForder));

            clmlList = wjmlList.stream().map(f -> f.getFwjlx1()).distinct().collect(Collectors.toList());
        }else {
            clmlList = CollectionUtil.newArrayList(selectMl);
        }

        if (CollectionUtils.isNotEmpty(clmlList)){
            Map clmlMap = null;
            Map wjjlMap = null;
            List<Map> wjjlMapList = null;
            List<WjjlVo> wjjlVoList = new ArrayList<>();
            for (String clml : clmlList){

                // 是否为指定目录
                if (StringUtils.isNotBlank(selectMl) && !clml.contains(selectMl)){
                    continue;
                }

                /*if (!clml.contains(WjmlConstant.wjml_type_1_xcjccl)){
                    continue;
                }*/

                clmlMap = new HashMap();
                wjjlMapList = new ArrayList<>();
                clmlMap.put("mlmc", clml); // 目录

                // 获取目录下的所以类型
                /*List<SpcySqsxWjml> lxmcList = new ArrayList<>();
                if (CollectionUtils.isNotEmpty(wjmlList)){
                    lxmcList = wjmlList.stream()
                            .filter(f -> StringUtils.isNotBlank(f.getFwjlx1())
                                    && f.getFwjlx1().equals(clml))
                            .collect(Collectors.toList());
                }

                List<String> lxmcMlList = new ArrayList<>();
                if (CollectionUtils.isNotEmpty(lxmcList)){
                    lxmcMlList = lxmcList.stream().map(f -> f.getFlxmc()).distinct().collect(Collectors.toList());
                }*/
                List<String> lxmcMlList = spcySqsxWjmlService.selectFlxmcListByCodeAndFwjlx1(fywflcode, clml);

                // 对应目录下的类型文件
                if (CollectionUtils.isNotEmpty(lxmcMlList)){
                    for (String lxmc : lxmcMlList){
                        wjjlMap = new HashMap();

                        /** 合并事项 */
                        if (hbsxidVo.getFhbsxzt() != null
                                && hbsxidVo.getFhbsxzt() == 1
                                && (lxmc.equals(WjmlConstant.xcjcbg) || lxmc.equals(WjmlConstant.xcjcqxjlb))){
                            List<SpcySqsxWjjl> hbsxWjList = spcySqsxWjjlService.list(new LambdaQueryWrapper<SpcySqsxWjjl>()
                                    .in(SpcySqsxWjjl::getFpid, StringPatternUtils.getListByIds(hbsxidVo.getFhbsxids()))
                                    .eq(SpcySqsxWjjl::getFwjlx1, lxmc));

                            wjjlMap.put("clmc", lxmc); // 目录对应类型
                            wjjlMap.put("wjList", hbsxWjList); // 目录对应类型文件
                            wjjlMapList.add(wjjlMap);

                            continue;
                        }

                        /** 非合并事项 */
                        if (CollectionUtils.isNotEmpty(wjjlList)) {

                            // 是否只显示除了文档以外的文件
                            if (isSelectPdf) {
                                wjjlVoList = wjjlList.stream()
                                        .filter(f -> StringUtils.isNotBlank(f.getFwjlx1())
                                                && f.getFwjlx1().equals(lxmc)
                                                && StringUtils.isNotBlank(f.getFwjlx())
                                                && !f.getFwjlx().equals(".doc")
                                                && !f.getFwjlx().equals(".docx"))
                                        .collect(Collectors.toList());
                            }else {
                                wjjlVoList = wjjlList.stream()
                                        .filter(f -> StringUtils.isNotBlank(f.getFwjlx1())
                                                && f.getFwjlx1().equals(lxmc))
                                        .collect(Collectors.toList());
                            }
                        }else {
                            wjjlVoList = new ArrayList<>();
                        }

                        wjjlMap.put("clmc", lxmc); // 目录对应类型
                        wjjlMap.put("wjList", wjjlVoList); // 目录对应类型文件
                        wjjlMapList.add(wjjlMap);
                    }
                }

                clmlMap.put("mcList", wjjlMapList); // 目录文件list
                reusltMapList.add(clmlMap);
            }
        }

        return reusltMapList;
    }

    /**
     * 获取文件记录-根据文件类型分组
     * @param sqsxid 申请事项id
     * @param typeList 文件类型数组
     */
    public List<Map> selectGroupWjjlByFtype(String sqsxid, List<String> typeList){
        return this.selectGroupWjjlByFtype(sqsxid, typeList, false);
    }

    /**
     * 获取文件记录-根据文件类型分组
     * @param sqsxid 申请事项id
     * @param typeList 文件类型数组
     * @param isSelecyMyWj true: 只查询个人上传的附件
     */
    public List<Map> selectGroupWjjlByFtype(String sqsxid, List<String> typeList, boolean isSelecyMyWj){
        List<Map> reusltMapList = new ArrayList<>();

        List<SpcySqsxWjjl> wjList = new ArrayList<>();
        if (isSelecyMyWj){
            wjList = spcySqsxWjjlService.findWjjlListByFwjlx1AndFwjlx7(typeList, sqsxid, GetCurrentUserUtils.getCurrentUserId());
        }else {
            wjList = spcySqsxWjjlService.findWjjlListByFwjlx1(typeList, sqsxid);
        }

        for (String type : typeList){
            HashMap clmlMap = new HashMap();
            clmlMap.put("clmc", type);

            List<SpcySqsxWjjl> filterList = wjList.stream()
                    .filter(f -> f.getFwjlx1().equals(type))
                    .collect(Collectors.toList());

            clmlMap.put("wjList", filterList);


            reusltMapList.add(clmlMap);
        }

        return reusltMapList;
    }

    /**
     * 获取APP端-方案制定函材料
     * @param sqsxid
     * @return
     */
    public List<Map> selectAppFazhFileList(String sqsxid){
        List<String> typeList = CollectionUtil.newArrayList(
                WjmlConstant.jcfa,
                WjmlConstant.xpxcscydh,
                WjmlConstant.xpxcgcydh,
                WjmlConstant.qydwjctz,
                WjmlConstant.jczgclyq,
                WjmlConstant.sycfhxjcbg
        );

        List<Map> reusltMapList = new ArrayList<>();

        List<SpcySqsxWjjl> wjList = spcySqsxWjjlService.findWjjlListByFwjlx1(typeList, sqsxid);
        for (String type : typeList){

            HashMap clmlMap = new HashMap();

            if (type.equals(WjmlConstant.sycfhxjcbg)) {
                clmlMap.put("clmc", type);
                clmlMap.put("wjList", getSycjcbgLists(sqsxid, wjList));
            }else {

                String finalType = type;
                clmlMap.put("wjList", wjList.stream()
                        .filter(f -> f.getFwjlx1().equals(finalType))
                        .collect(Collectors.toList()));

                if (type.equals(WjmlConstant.xpxcscydh)) {
                    type = "检查员函";
                } else if (type.equals(WjmlConstant.xpxcgcydh)) {
                    type = "观察员函";
                } else if (type.equals(WjmlConstant.qydwjctz)) {
                    type = "企业通知函";
                }
                clmlMap.put("clmc", type);

            }
            reusltMapList.add(clmlMap);
        }

        return reusltMapList;
    }

    /**
     * 获取PC端-综合评定材料
     * @param sqsxid
     * @return
     */
    public List<Map> selectZhpdclMlList(String sqsxid){

        List<String> typeList = CollectionUtil.newArrayList(
                WjmlConstant.zhpdshj,
                WjmlConstant.zhpdbgs,
                WjmlConstant.yshmb
        );

        List<Map> reusltMapList = new ArrayList<>();

        List<SpcySqsxWjjl> wjList = spcySqsxWjjlService.findWjjlListByFwjlx1(typeList, sqsxid);
        for (String type : typeList){

            HashMap clmlMap = new HashMap();
            String finalType = type;
            clmlMap.put("wjList", wjList.stream()
                    .filter(f -> f.getFwjlx1().equals(finalType))
                    .collect(Collectors.toList()));

            clmlMap.put("clmc", type);

            /** 获取综合评定审核件-相关打印签名数据 */
            if (type.equals(WjmlConstant.zhpdshj)) {
                clmlMap.putAll(this.zhpdShjPrintData(sqsxid));
            }

            reusltMapList.add(clmlMap);
        }

        return reusltMapList;
    }

    /**
     * 打包下载zip-中心材料相关企业材料(现场检查移送企业材料)
     * @param typeList 文件类型列表
     */
    public void downZipZxclQy(String sqsxid, List<String> typeList, HttpServletResponse response)  {
        List<InputStream> files = new ArrayList<>();
        List<String> fileNames = new ArrayList<>();
        List<SpcySqsxWjjl> wjList = spcySqsxWjjlService.findWjjlListByFwjlx1(typeList, sqsxid);
        for (SpcySqsxWjjl wj : wjList){

            if (StringUtils.isBlank(wj.getFcflj())){
                continue;
            }

            if (localFileUploadUtils.isEnableMinioWjjlFile()){
                InputStream minioInput = spcyMinioUtils.getObject(localFileUploadUtils.getSqsxWjjlRelativeUrl(wj.getFcflj()));
                if (minioInput != null) {
                    fileNames.add(wj.getFwjxsmc()+wj.getFwjlx());
                    files.add(minioInput);
                }
            }else {
                File file = new File(localFileUploadUtils.getSqsxWjjlAbsUrl(wj.getFcflj()));
                if (file.exists()) {
                    try {
                        fileNames.add(wj.getFwjxsmc() + wj.getFwjlx());
                        files.add(new FileInputStream(file.getAbsoluteFile()));
                    }catch (FileNotFoundException e){
                        log.error("打包下载zip-中心材料相关企业材料: {}", e.getMessage());
                    }
                }
            }

        }

        /** 现场检查纪律带签字模板 */
        String xcjcjlTemplateUrl = sqsxYwxxInfoService.genSqsxHyXcjcjlTemplate(sqsxid);
        if (StringUtils.isNotBlank(xcjcjlTemplateUrl) && (new File(xcjcjlTemplateUrl).exists())){
            fileNames.add(WjmlConstant.xcjcjl+"-"+"签章模板"+ Util.getWjHz(xcjcjlTemplateUrl));
            try {
                files.add(new FileInputStream(xcjcjlTemplateUrl));
            } catch (FileNotFoundException e) {
                log.error("打包下载zip-中心材料相关企业材料-现场检查纪律带签字模板: {}", e.getMessage());
            }
        }

        /** 相关模板文件 */
        List<String> typeMbList = CollectionUtil.newArrayList(
                WjmlConstant.ljzlfk_cj,
                WjmlConstant.ljzlfk_fcj,
                WjmlConstant.cjcygzlfzdcb
        );
        SpcySqsxBasic sqsx = spcySqsxBaiscService.getById(sqsxid);
        for (String mbType : typeMbList){
            String mbUrl = sqsxYwxxInfoService.downRsWjmbToLocal("", sqsxid, mbType, sqsx, true);
            if (StringUtils.isNotBlank(mbUrl)){
                fileNames.add( mbType + "-模板" + Util.getWjHz(mbUrl));
                try {
                    files.add(new FileInputStream(mbUrl));
                } catch (FileNotFoundException e) {
                    log.error("打包下载zip-中心材料相关企业材料-相关模板文件错误: {}-类型-{}", mbType, e.getMessage());
                }
            }
        }

        /** 顾客满意度调查问卷-二维码 */
        try {
            String mydQrCodeUrl = localFileUploadUtils.getTempFileRootUrl() + "/" + GetUuIdUtils.ReplaceUuId() + ".png";
            QRCodeService.generateSqsxHyMydQRCode(sqsxid, mydQrCodeUrl);

            fileNames.add("广西食品药品审评查验中心顾客满意度调查问卷二维码.png");
            files.add(new FileInputStream(mydQrCodeUrl));

        }catch (IOException e){
            log.error("打包下载zip-企业相关文件目录(办理材料)-生成满意度调查表二维码-IOException错误：{}", e.getMessage());
        }catch (WriterException e){
            log.error("打包下载zip-企业相关文件目录(办理材料)-生成满意度调查表二维码-WriterException错误：{}", e.getMessage());
        }

        /** 现场检查会议-末次会议签到码-二维码 */
        /*List<SpcySqsxHyzt> hyList = spcySqsxHyztMapper.selectList(new LambdaQueryWrapper<SpcySqsxHyzt>()
                .eq(SpcySqsxHyzt::getFsqsxid, sqsxid)
                .eq(SpcySqsxHyzt::getFhylx, SqsxHyConstant.HYLX_MCHY));
        if (CollectionUtil.isNotEmpty(hyList)) {
            try {
                String mydQrCodeUrl = localFileUploadUtils.getTempFileRootUrl() + "/" + GetUuIdUtils.ReplaceUuId() + ".png";
                QRCodeService.generateSqsxXcjcMshyQRCode(sqsxid, mydQrCodeUrl, hyList.get(0));

                fileNames.add("现场检查-末次会议签到码.png");
                files.add(new FileInputStream(mydQrCodeUrl));

            } catch (IOException e) {
                log.error("打包下载zip-企业相关文件目录(办理材料)-末次会议签到码-IOException错误：{}", e.getMessage());
            } catch (WriterException e) {
                log.error("打包下载zip-企业相关文件目录(办理材料)-末次会议签到码-WriterException错误：{}", e.getMessage());
            }
        }*/

        PdfUtils.downInputZip(files, fileNames, response);
    }

    /**
     * 勾选打包下载zip-现场检查移送企业材料
     */
    public void downZipZxclQyByWjIds(String sqsxid, String wjids, HttpServletResponse response){

        List<InputStream> files = new ArrayList<>();
        List<String> fileNames = new ArrayList<>();

        List<String> listByIds = StringPatternUtils.getListByIds(wjids);
        List<SpcySqsxWjjl> wjList = spcySqsxWjjlService.listByIds(listByIds);
        for (SpcySqsxWjjl wj : wjList){
            if (StringUtils.isBlank(wj.getFcflj())){
                continue;
            }

            if (localFileUploadUtils.isEnableMinioWjjlFile()){
                InputStream minioInput = spcyMinioUtils.getObject(localFileUploadUtils.getSqsxWjjlRelativeUrl(wj.getFcflj()));
                if (minioInput != null) {
                    fileNames.add(wj.getFwjxsmc()+wj.getFwjlx());
                    files.add(minioInput);
                }
            }else {
                File file = new File(localFileUploadUtils.getSqsxWjjlAbsUrl(wj.getFcflj()));
                if (file.exists()) {
                    try {
                        fileNames.add(wj.getFwjxsmc() + wj.getFwjlx());
                        files.add(new FileInputStream(file.getAbsoluteFile()));
                    }catch (FileNotFoundException e){
                        log.error("勾选打包下载zip-中心材料相关企业材料: {}", e.getMessage());
                    }
                }
            }
        }

        /** 勾选文件id的模板id */
        List<String> typeMbList = new ArrayList<>();
        for (String gxId : listByIds){
            if (!gxId.contains("@")){
              continue;
            }

            String[] splitWjIds = gxId.split("@");

            /** 现场检查纪律带签字模板 */
            if (splitWjIds[0].equals(WjmlConstant.xcjcjl)) {
                String xcjcjlTemplateUrl = sqsxYwxxInfoService.genSqsxHyXcjcjlTemplate(sqsxid);
                if (StringUtils.isNotBlank(xcjcjlTemplateUrl) && (new File(xcjcjlTemplateUrl).exists())) {
                    fileNames.add(WjmlConstant.xcjcjl + "-" + "签章模板" + Util.getWjHz(xcjcjlTemplateUrl));
                    try {
                        files.add(new FileInputStream(xcjcjlTemplateUrl));
                    } catch (FileNotFoundException e) {
                        log.error("打包下载zip-中心材料相关企业材料-现场检查纪律带签字模板: {}", e.getMessage());
                    }
                }
            }else if (splitWjIds[0].equals(WjmlConstant.ljzlfk_cj)){

                typeMbList.add(WjmlConstant.ljzlfk_cj);
            }else if (splitWjIds[0].equals(WjmlConstant.ljzlfk_fcj)){

                typeMbList.add(WjmlConstant.ljzlfk_fcj);
            }else if (splitWjIds[0].equals(WjmlConstant.cjcygzlfzdcb)){

                typeMbList.add(WjmlConstant.cjcygzlfzdcb);
            }
        }

        if (CollectionUtil.isNotEmpty(typeMbList)) {
            SpcySqsxBasic sqsx = spcySqsxBaiscService.getById(sqsxid);
            for (String mbType : typeMbList) {
                String mbUrl = sqsxYwxxInfoService.downRsWjmbToLocal("", sqsxid, mbType, sqsx, true);
                if (StringUtils.isNotBlank(mbUrl)) {
                    fileNames.add(mbType + "-模板" + Util.getWjHz(mbUrl));
                    try {
                        files.add(new FileInputStream(mbUrl));
                    } catch (FileNotFoundException e) {
                        log.error("打包下载zip-中心材料相关企业材料-相关模板文件错误: {}-类型-{}", mbType, e.getMessage());
                    }
                }
            }
        }

        /** 顾客满意度调查问卷-二维码 */
        try {
            String mydQrCodeUrl = localFileUploadUtils.getTempFileRootUrl() + "/" + GetUuIdUtils.ReplaceUuId() + ".png";
            QRCodeService.generateSqsxHyMydQRCode(sqsxid, mydQrCodeUrl);

            fileNames.add("广西食品药品审评查验中心顾客满意度调查问卷二维码.png");
            files.add(new FileInputStream(mydQrCodeUrl));

        }catch (IOException e){
            log.error("勾选打包下载zip-现场检查移送企业材料-生成满意度调查表二维码-IOException错误：{}", e.getMessage());
        }catch (WriterException e){
            log.error("勾选打包下载zip-现场检查移送企业材料-生成满意度调查表二维码-WriterException错误：{}", e.getMessage());
        }

        /** 现场检查会议-末次会议签到码-二维码 */
        /*List<SpcySqsxHyzt> hyList = spcySqsxHyztMapper.selectList(new LambdaQueryWrapper<SpcySqsxHyzt>()
                .eq(SpcySqsxHyzt::getFsqsxid, sqsxid)
                .eq(SpcySqsxHyzt::getFhylx, SqsxHyConstant.HYLX_MCHY));
        if (CollectionUtil.isNotEmpty(hyList)) {
            try {
                String mydQrCodeUrl = localFileUploadUtils.getTempFileRootUrl() + "/" + GetUuIdUtils.ReplaceUuId() + ".png";
                QRCodeService.generateSqsxXcjcMshyQRCode(sqsxid, mydQrCodeUrl, hyList.get(0));

                fileNames.add("现场检查-末次会议签到码.png");
                files.add(new FileInputStream(mydQrCodeUrl));

            } catch (IOException e) {
                log.error("打包下载zip-企业相关文件目录(办理材料)-末次会议签到码-IOException错误：{}", e.getMessage());
            } catch (WriterException e) {
                log.error("打包下载zip-企业相关文件目录(办理材料)-末次会议签到码-WriterException错误：{}", e.getMessage());
            }
        }*/

        PdfUtils.downInputZip(files, fileNames, response);
    }

    /**
     * 获取首次会议-企业相关文件目录(办理材料)(现场检查移送企业材料 目录)
     */
    public HttpResult selectWjMlBySchyQy(String sqsxid){
        List<String> typeList = CollectionUtil.newArrayList(
                WjmlConstant.qydwjctz,
                WjmlConstant.jczgclyq
        );
//        List<Map> reusltMapList = this.selectGroupWjjlByFtype(sqsxid, typeList);
        List<Map> reusltMapList = new ArrayList<>();

        List<String> filterTypeList = CollectionUtil.newArrayList(
                WjmlConstant.qydwjctz,
                WjmlConstant.jczgclyq,
                WjmlConstant.xcjcjl,
                WjmlConstant.ljzlfk_cj,
                WjmlConstant.ljzlfk_fcj,
                WjmlConstant.cjcygzlfzdcb
        );
        List<SpcySqsxWjjl> filterWjList = spcySqsxWjjlService.findWjjlListByFwjlx1(filterTypeList, sqsxid);

        for (String type : typeList) {
            HashMap clmlMap = new HashMap();
            clmlMap.put("clmc", type);

            List<SpcySqsxWjjl> filterList = filterWjList.stream()
                    .filter(f -> f.getFwjlx1().equals(type))
                    .collect(Collectors.toList());

            clmlMap.put("wjList", filterList);
            reusltMapList.add(clmlMap);
        }


        /** 0 */
        HashMap clmlMap = new HashMap();
        clmlMap.put("clmc", WjmlConstant.xcjcjl);

        List<SpcySqsxWjjl> filterList = new ArrayList<>(filterWjList.stream()
                .filter(f -> f.getFwjlx1().equals(WjmlConstant.xcjcjl))
                .collect(Collectors.toList()));

        filterList.add(spcySqsxWjjlService.genDefinedWjjlWithUnReal(sqsxid, WjmlConstant.xcjcjl, ".docx", WjmlConstant.xcjcjl+"-模板"));

        clmlMap.put("wjList", filterList);
        reusltMapList.add(clmlMap);

        /** 1 */
        HashMap clmlMap1 = new HashMap();
        clmlMap1.put("clmc", WjmlConstant.ljzlfk_cj);

        List<SpcySqsxWjjl> filterList1 = new ArrayList<>(filterWjList.stream()
                .filter(f -> f.getFwjlx1().equals(WjmlConstant.ljzlfk_cj))
                .collect(Collectors.toList()));

        filterList1.add(spcySqsxWjjlService.genDefinedWjjlWithUnReal(sqsxid, WjmlConstant.ljzlfk_cj, ".docx", WjmlConstant.ljzlfk_cj+"-模板"));
        clmlMap1.put("wjList", filterList1);
        reusltMapList.add(clmlMap1);

        /** 2 */
        HashMap clmlMap2 = new HashMap();
        clmlMap2.put("clmc", WjmlConstant.ljzlfk_fcj);

        List<SpcySqsxWjjl> filterList2 = new ArrayList<>(filterWjList.stream()
                .filter(f -> f.getFwjlx1().equals(WjmlConstant.ljzlfk_fcj))
                .collect(Collectors.toList()));

        filterList2.add(spcySqsxWjjlService.genDefinedWjjlWithUnReal(sqsxid, WjmlConstant.ljzlfk_fcj, ".docx", WjmlConstant.ljzlfk_fcj+"-模板"));

        clmlMap2.put("wjList", filterList2);
        reusltMapList.add(clmlMap2);

        /** 3 */
        HashMap clmlMap3 = new HashMap();
        clmlMap3.put("clmc", WjmlConstant.cjcygzlfzdcb);

        List<SpcySqsxWjjl> filterList3 = new ArrayList<>(filterWjList.stream()
                .filter(f -> f.getFwjlx1().equals(WjmlConstant.cjcygzlfzdcb))
                .collect(Collectors.toList()));

        filterList3.add(spcySqsxWjjlService.genDefinedWjjlWithUnReal(sqsxid, WjmlConstant.cjcygzlfzdcb, ".docx", WjmlConstant.cjcygzlfzdcb+"-模板"));

        clmlMap3.put("wjList", filterList3);
        reusltMapList.add(clmlMap3);

        /** 4 */
        HashMap clmlMap4 = new HashMap();
        clmlMap4.put("clmc", WjmlConstant.myddcwjQrCode);

        List<SpcySqsxWjjl> filterList4 = new ArrayList<>(1);
        filterList4.add(spcySqsxWjjlService.genDefinedWjjlWithUnReal(sqsxid, WjmlConstant.myddcwjQrCode, ".png", WjmlConstant.myddcwjQrCode));

        clmlMap4.put("wjList", filterList4);
        reusltMapList.add(clmlMap4);

        /** 5 */
        /*HashMap clmlMap5 = new HashMap();
        clmlMap5.put("clmc", WjmlConstant.xcjcMshyQrCode);

        List<SpcySqsxWjjl> filterList5 = new ArrayList<>(1);
        filterList5.add(spcySqsxWjjlService.genDefinedWjjlWithUnReal(sqsxid, WjmlConstant.xcjcMshyQrCode, ".png", WjmlConstant.xcjcMshyQrCode));

        clmlMap5.put("wjList", filterList5);
        reusltMapList.add(clmlMap5);*/

        return HttpResultVo.HttpResultList(reusltMapList);
    }

    /**
     * 下载企业相关文件目录-材料（自定义文件）
     */
    public void downWjmlBySchyQy(String wjId, HttpServletResponse response) throws IOException {
        String[] splitWjId = wjId.split("@");

        if (splitWjId[0].equals(WjmlConstant.xcjcjl)){

            String xcjcjlTemplateUrl = sqsxYwxxInfoService.genSqsxHyXcjcjlTemplate(splitWjId[1]);
            if (StringUtils.isBlank(xcjcjlTemplateUrl)){
                response.setContentType("text/html");
                response.setCharacterEncoding("UTF-8");
                PrintWriter writer = response.getWriter();
                writer.println(WjmlConstant.xcjcjl + "模板错误！");
                writer.close(); // 不要忘记关闭PrintWriter

                return;
            }

            localFileUploadUtils.ejzLocalFileByZdyFile(new File(xcjcjlTemplateUrl), response);

        }else if (splitWjId[0].equals(WjmlConstant.myddcwjQrCode)){

            /** 顾客满意度调查问卷二维码 */
            try {
                String mydQrCodeUrl = localFileUploadUtils.getTempFileRootUrl() + "/" + GetUuIdUtils.ReplaceUuId() + ".png";
                QRCodeService.generateSqsxHyMydQRCode(splitWjId[1], mydQrCodeUrl);

                localFileUploadUtils.ejzLocalFileByZdyFile(new File(mydQrCodeUrl), response);
            }catch (Exception e){
                response.setContentType("text/html");
                response.setCharacterEncoding("UTF-8");
                PrintWriter writer = response.getWriter();
                writer.println(WjmlConstant.myddcwjQrCode + "生成模板错误！");
                writer.close(); // 不要忘记关闭PrintWriter

                return;
            }

        }else if (splitWjId[0].equals(WjmlConstant.xcjcMshyQrCode)){
            /** 现场检查会议-末次会议签到码-二维码 */
            List<SpcySqsxHyzt> hyList = spcySqsxHyztMapper.selectList(new LambdaQueryWrapper<SpcySqsxHyzt>()
                    .eq(SpcySqsxHyzt::getFsqsxid, splitWjId[1])
                    .eq(SpcySqsxHyzt::getFhylx, SqsxHyConstant.HYLX_MCHY));
            if (CollectionUtil.isEmpty(hyList)){
                response.setContentType("text/html");
                response.setCharacterEncoding("UTF-8");
                PrintWriter writer = response.getWriter();
                writer.println("没有末次会议数据！");
                writer.close(); // 不要忘记关闭PrintWriter

                return;
            }

            try {
                String mshyQrCodeUrl = localFileUploadUtils.getTempFileRootUrl() + "/" + GetUuIdUtils.ReplaceUuId() + ".png";
                QRCodeService.generateSqsxXcjcMshyQRCode(splitWjId[1], mshyQrCodeUrl, hyList.get(0));

                localFileUploadUtils.ejzLocalFileByZdyFile(new File(mshyQrCodeUrl), response);

            } catch (Exception e) {
                response.setContentType("text/html");
                response.setCharacterEncoding("UTF-8");
                PrintWriter writer = response.getWriter();
                writer.println(WjmlConstant.xcjcMshyQrCode + "生成模板错误！");
                writer.close(); // 不要忘记关闭PrintWriter

                return;
            }

        }else {

            SpcySqsxBasic sqsx = spcySqsxBaiscService.getById(splitWjId[1]);

            String mbUrl = sqsxYwxxInfoService.downRsWjmbToLocal("", splitWjId[1], splitWjId[0], sqsx, true);
            if (StringUtils.isBlank(mbUrl)){
                response.setContentType("text/html");
                response.setCharacterEncoding("UTF-8");
                PrintWriter writer = response.getWriter();
                writer.println(splitWjId[0] + "模板错误！");
                writer.close(); // 不要忘记关闭PrintWriter

                return;
            }

            localFileUploadUtils.ejzLocalFileByZdyFile(new File(mbUrl), response);

        }
    }

    /**
     * 打包下载-经办事项所以材料
     */
    public void downZipJcfaCl(String sqsxid, HttpServletResponse response) throws IOException {
        if (StringUtils.isBlank(sqsxid)) {
            response.setContentType("text/html");
            response.setCharacterEncoding("UTF-8");
            PrintWriter writer = response.getWriter();
            writer.println("事项参数为空！");
            writer.close(); // 不要忘记关闭PrintWriter

            return;
        }
        List<String> typeList = CollectionUtil.newArrayList(
                WjmlConstant.jcfa,
                WjmlConstant.xpxcscydh,
                WjmlConstant.xpxcgcydh,
                WjmlConstant.qydwjctz,
                WjmlConstant.jczgclyq,
                WjmlConstant.sycfhxjcbg
        );
        List<SpcySqsxWjjl> wjList = spcySqsxWjjlService.findWjjlListByFwjlx1(typeList, sqsxid);

        List<InputStream> files = new ArrayList<>();
        List<String> fileNames = new ArrayList<>();

        for (SpcySqsxWjjl wj : wjList){
            if (StringUtils.isNotBlank(wj.getFwjlx1()) && wj.getFwjlx1().equals(WjmlConstant.sycfhxjcbg)){
                continue;
            }
            if (StringUtils.isBlank(wj.getFcflj())){
                continue;
            }

            if (localFileUploadUtils.isEnableMinioWjjlFile()){
                InputStream minioInput = spcyMinioUtils.getObject(localFileUploadUtils.getSqsxWjjlRelativeUrl(wj.getFcflj()));
                if (minioInput != null) {
                    fileNames.add(wj.getFwjxsmc()+wj.getFwjlx());
                    files.add(minioInput);
                }
            }else {
                File file = new File(localFileUploadUtils.getSqsxWjjlAbsUrl(wj.getFcflj()));
                if (file.exists()) {
                    try {
                        fileNames.add(wj.getFwjxsmc() + wj.getFwjlx());
                        files.add(new FileInputStream(file.getAbsoluteFile()));
                    }catch (FileNotFoundException e){
                        log.error("勾选打包下载zip-经办事项所以材料: {}", e.getMessage());
                    }
                }
            }
        }

        // 上一次符合性检查报告
        List<SpcySqsxWjjl> sycjcbgLists = this.getSycjcbgLists(sqsxid, wjList);
        for (SpcySqsxWjjl wj : sycjcbgLists){
            if (StringUtils.isBlank(wj.getFcflj())){
                continue;
            }

            if (localFileUploadUtils.isEnableMinioWjjlFile()){
                InputStream minioInput = spcyMinioUtils.getObject(localFileUploadUtils.getSqsxWjjlRelativeUrl(wj.getFcflj()));
                if (minioInput != null) {
                    fileNames.add(wj.getFwjxsmc()+wj.getFwjlx());
                    files.add(minioInput);
                }
            }else {
                File file = new File(localFileUploadUtils.getSqsxWjjlAbsUrl(wj.getFcflj()));
                if (file.exists()) {
                    try {
                        fileNames.add(wj.getFwjxsmc() + wj.getFwjlx());
                        files.add(new FileInputStream(file.getAbsoluteFile()));
                    }catch (FileNotFoundException e){
                        log.error("勾选打包下载zip-经办事项所以材料: {}", e.getMessage());
                    }
                }
            }
        }

        PdfUtils.downInputZip(files, fileNames, response);
    }

    /**
     * 获取综合评定审核件-相关打印签名数据
     * */
    public Map zhpdShjPrintData(String sqsxid){
        Map resultMap = new HashMap();

        /** 事项打印信息 */
        SqsxShjPrintDataDto sqsx = spcySqsxBaiscService.selectSqsxShjPrintById(sqsxid);
        SqsxShjPrintDataVo printDataVo = null;
        if (sqsx != null){
            printDataVo = new SqsxShjPrintDataVo();
            printDataVo.setId(sqsx.getId());
            printDataVo.setFsxmc(sqsx.getFsxmc());
            printDataVo.setFqymc(sqsx.getFqymc());
            printDataVo.setFslbh(sqsx.getFslbh());
            printDataVo.setFpdbh(sqsx.getFpdbh());
            printDataVo.setFyshbh(sqsx.getFyshbh());
            printDataVo.setFjclx(sqsx.getFjclx());
            printDataVo.setFcyjcfw(sqsx.getFcyjcfw());
            printDataVo.setFzhpdyj(sqsx.getFzhpdyj());
            printDataVo.setFslsj(sqsx.getFslsj() != null ? DateConversionUtils.DateToStringNew(sqsx.getFslsj()) : "");
            printDataVo.setFcykssj(sqsx.getFcykssj() != null ? DateConversionUtils.DateToStringNew(sqsx.getFcykssj()) : "");
            printDataVo.setFcyjssj(sqsx.getFcyjssj() != null ? DateConversionUtils.DateToStringNew(sqsx.getFcyjssj()) : "");

        }
        resultMap.put("sqsx", printDataVo);

        /** 审查人 */
        List<String> hjmcList = new ArrayList<>(4);

        // 待办环节
        List<String> dbFhjmcList = spcySqsxJobMapper.selectDbFhjmcListByFpid(sqsxid, SpcyFlowConstant.LCMC_LIKE_SPCYLC);
        if (CollectionUtil.isNotEmpty(dbFhjmcList)){
            if (dbFhjmcList.contains(SpcyFlowConstant.HJMC_ZHPD)){
                resultMap.put("scrQm", new ZhpdshjPrintDataVo());
                resultMap.put("fhrQmList", new ArrayList<>(1));
                resultMap.put("ksfzrQm", new ZhpdshjPrintDataVo());
                resultMap.put("zxldQm", new ZhpdshjPrintDataVo());

                return resultMap;
            }
            else if (dbFhjmcList.contains(SpcyFlowConstant.HJMC_ZHPD_FH)){
                hjmcList.add(SpcyFlowConstant.HJMC_ZHPD);
                hjmcList.add(SpcyFlowConstant.HJMC_ZHPD_FH);

            }
            else if (dbFhjmcList.contains(SpcyFlowConstant.HJMC_KSFH)){
                hjmcList.add(SpcyFlowConstant.HJMC_ZHPD);
                hjmcList.add(SpcyFlowConstant.HJMC_ZHPD_FH);

            }
            else if (dbFhjmcList.contains(SpcyFlowConstant.HJMC_YSQR)){
                hjmcList.add(SpcyFlowConstant.HJMC_ZHPD);
                hjmcList.add(SpcyFlowConstant.HJMC_ZHPD_FH);
                hjmcList.add(SpcyFlowConstant.HJMC_KSFH);

            }else {
                hjmcList.add(SpcyFlowConstant.HJMC_ZHPD);
                hjmcList.add(SpcyFlowConstant.HJMC_ZHPD_FH);
                hjmcList.add(SpcyFlowConstant.HJMC_KSFH);
                hjmcList.add(SpcyFlowConstant.HJMC_YSQR);
            }
        }else {
            hjmcList.add(SpcyFlowConstant.HJMC_ZHPD);
            hjmcList.add(SpcyFlowConstant.HJMC_ZHPD_FH);
            hjmcList.add(SpcyFlowConstant.HJMC_KSFH);
            hjmcList.add(SpcyFlowConstant.HJMC_YSQR);
        }

        List<SpcySqsxJob> jobList = spcySqsxJobMapper.selectYbJobListByHjList(sqsxid, SpcyConstant.BLZT_YB, hjmcList);

        ZhpdshjPrintDataVo scrQm = new ZhpdshjPrintDataVo();
        List<ZhpdshjPrintDataVo> fhrQmList = new ArrayList<>(jobList.size());
        ZhpdshjPrintDataVo ksfzrQm = new ZhpdshjPrintDataVo();
        ZhpdshjPrintDataVo zxldQm = new ZhpdshjPrintDataVo();

        List<String> fhrIdList = new ArrayList<>(jobList.size());
        for (String hj : hjmcList){

            if (hj.equals(SpcyFlowConstant.HJMC_ZHPD)){
                if (scrQm == null) {
                    scrQm = new ZhpdshjPrintDataVo();
                }

                List<SpcySqsxJob> filterList = jobList.stream()
                        .filter(f -> f.getFhjmc().equals(hj))
                        .collect(Collectors.toList());
                if (CollectionUtil.isNotEmpty(filterList)) {
                    /** 筛选办理人 */
                    List<UserRealnameVo> userList = ycSysUserMapper.findRealnameListsByIds(StringPatternUtils.getListByIds(filterList.get(0).getFdbrid()));
                    List<UserRealnameVo> filterUserList = userList.stream()
                            .filter(f -> StringUtils.isNotBlank(filterList.get(0).getFblr())
                                    && f.getRealname().equals(filterList.get(0).getFblr()))
                            .collect(Collectors.toList());
                    if (CollectionUtil.isNotEmpty(filterUserList)){
                        scrQm.setFuserid(filterUserList.get(0).getId());
                        scrQm.setName(filterUserList.get(0).getRealname());
                        scrQm.setQmpath(spcyMyInfoService.genUserSignLink(scrQm.getFuserid()));
                    }

                    scrQm.setOpinion(filterList.get(0).getFblyj());
                    if (filterList.get(0).getFblsj() != null) {
                        scrQm.setQmrq(DateConversionUtils.DateToStringNew(filterList.get(0).getFblsj()));
                    }
                }
            }

            if (hj.equals(SpcyFlowConstant.HJMC_ZHPD_FH)){
                List<SpcySqsxJob> filterList = jobList.stream()
                        .filter(f -> f.getFhjmc().equals(hj))
                        .collect(Collectors.toList());
                for (SpcySqsxJob fhFilter : filterList){

                    /** 筛选办理人 */
                    List<UserRealnameVo> userList = ycSysUserMapper.findRealnameListsByIds(StringPatternUtils.getListByIds(fhFilter.getFdbrid()));
                    List<UserRealnameVo> filterUserList = userList.stream()
                            .filter(f -> StringUtils.isNotBlank(fhFilter.getFblr())
                                    && f.getRealname().equals(fhFilter.getFblr()))
                            .collect(Collectors.toList());
                    if (CollectionUtil.isEmpty(filterUserList)){
                        continue;
                    }

                    // 存在跳出
                    if (fhrIdList.contains(filterUserList.get(0).getId())){
                        continue;
                    }

                    ZhpdshjPrintDataVo fhrQm = new ZhpdshjPrintDataVo();
                    fhrQm.setFuserid(filterUserList.get(0).getId());
                    fhrQm.setName(filterUserList.get(0).getRealname());
                    fhrQm.setQmpath(spcyMyInfoService.genUserSignLink(fhrQm.getFuserid()));

                    fhrQm.setOpinion(fhFilter.getFblyj());
                    if (fhFilter.getFblsj() != null) {
                        fhrQm.setQmrq(DateConversionUtils.DateToStringNew(fhFilter.getFblsj()));
                    }

                    fhrIdList.add(fhrQm.getFuserid());
                    fhrQmList.add(fhrQm);
                }
            }

            if (hj.equals(SpcyFlowConstant.HJMC_KSFH)){
                if (ksfzrQm == null) {
                    ksfzrQm = new ZhpdshjPrintDataVo();
                }

                List<SpcySqsxJob> filterList = jobList.stream()
                        .filter(f -> f.getFhjmc().equals(hj))
                        .collect(Collectors.toList());
                if (CollectionUtil.isNotEmpty(filterList)) {
                    /** 筛选办理人 */
                    List<UserRealnameVo> userList = ycSysUserMapper.findRealnameListsByIds(StringPatternUtils.getListByIds(filterList.get(0).getFdbrid()));
                    List<UserRealnameVo> filterUserList = userList.stream()
                            .filter(f -> StringUtils.isNotBlank(filterList.get(0).getFblr())
                                    && f.getRealname().equals(filterList.get(0).getFblr()))
                            .collect(Collectors.toList());
                    if (CollectionUtil.isNotEmpty(filterUserList)){
                        ksfzrQm.setFuserid(filterUserList.get(0).getId());
                        ksfzrQm.setName(filterUserList.get(0).getRealname());
                        ksfzrQm.setQmpath(spcyMyInfoService.genUserSignLink(ksfzrQm.getFuserid()));
                    }

                    ksfzrQm.setOpinion(filterList.get(0).getFblyj());
                    if (filterList.get(0).getFblsj() != null) {
                        ksfzrQm.setQmrq(DateConversionUtils.DateToStringNew(filterList.get(0).getFblsj()));
                    }
                }
            }

            if (hj.equals(SpcyFlowConstant.HJMC_YSQR)){
                if (zxldQm == null) {
                    zxldQm = new ZhpdshjPrintDataVo();
                }

                List<SpcySqsxJob> filterList = jobList.stream()
                        .filter(f -> f.getFhjmc().equals(hj))
                        .collect(Collectors.toList());
                if (CollectionUtil.isNotEmpty(filterList)) {
                    /** 筛选办理人 */
                    List<UserRealnameVo> userList = ycSysUserMapper.findRealnameListsByIds(StringPatternUtils.getListByIds(filterList.get(0).getFdbrid()));
                    List<UserRealnameVo> filterUserList = userList.stream()
                            .filter(f -> StringUtils.isNotBlank(filterList.get(0).getFblr())
                                    && f.getRealname().equals(filterList.get(0).getFblr()))
                            .collect(Collectors.toList());
                    if (CollectionUtil.isNotEmpty(filterUserList)){
                        zxldQm.setFuserid(filterUserList.get(0).getId());
                        zxldQm.setName(filterUserList.get(0).getRealname());
                        zxldQm.setQmpath(spcyMyInfoService.genUserSignLink(zxldQm.getFuserid()));
                    }

                    zxldQm.setOpinion(filterList.get(0).getFblyj());
                    if (filterList.get(0).getFblsj() != null) {
                        zxldQm.setQmrq(DateConversionUtils.DateToStringNew(filterList.get(0).getFblsj()));
                    }
                }
            }
        }

        resultMap.put("scrQm", scrQm);
        resultMap.put("fhrQmList", fhrQmList);
        resultMap.put("ksfzrQm", ksfzrQm);
        resultMap.put("zxldQm", zxldQm);

        return resultMap;
    }

    /**
     * 查询日常监管-附件列表
     */
    public HttpResult selectRcjgFjMlWithCommon(String sqsxid){

        String code = spcySqsxBaiscService.getYwflCodeBySqsxId(sqsxid);

        if (code.equals(SpcyConstant.YWFL_CODE_YPSC)){
            return this.selectRcjgFjmlWithYpsc(sqsxid, code);
        }

        return this.selectRcjgFjmlWithYpsc(sqsxid, code);
    }

    /**
     * 查询日常监管-附件列表-药品生产
     */
    private HttpResult selectRcjgFjmlWithYpsc(String sqsxid, String code){

        List<String> typeList = spcySqsxWjmlService.selectFlxmcListByCodeAndFwjlx1(code, WjmlConstant.WJML_RCJG_MLLX);
        if (CollectionUtil.isEmpty(typeList)){
            typeList = CollectionUtil.newArrayList(
                    WjmlConstant.YPSC_RCJG_TCSM,
                    WjmlConstant.YPSC_RCJG_JCBLSMJ,
                    WjmlConstant.YPSC_RCJG_JCBGSMJ,
                    WjmlConstant.YPSC_RCJG_XCZFZ
            );
        }

        List<Map> reusltMapList = new ArrayList<>();

        List<SpcySqsxWjjl> wjList = spcySqsxWjjlService.findWjjlListByFwjlx1(typeList, sqsxid);
        for (String type : typeList){

            HashMap clmlMap = new HashMap();
            String finalType = type;
            clmlMap.put("wjList", wjList.stream()
                    .filter(f -> f.getFwjlx1().equals(finalType))
                    .collect(Collectors.toList()));

            clmlMap.put("clmc", type);

            reusltMapList.add(clmlMap);
        }

        return HttpResult.ok(reusltMapList);
    }

    /**
     * 获取企业撤件申请材料目录
     * @param sqsxid
     * @return
     */
    public List<Map> selectQycjsqclMlList(String sqsxid){

        List<String> typeList = CollectionUtil.newArrayList(
                WjmlConstant.qycjsqcl
        );

        List<Map> reusltMapList = new ArrayList<>();

        List<SpcySqsxWjjl> wjList = spcySqsxWjjlService.findWjjlListByFwjlx1(typeList, sqsxid);
        for (String type : typeList){

            HashMap clmlMap = new HashMap();
            String finalType = type;
            clmlMap.put("wjList", wjList.stream()
                    .filter(f -> f.getFwjlx1().equals(finalType))
                    .collect(Collectors.toList()));

            clmlMap.put("clmc", type);

            reusltMapList.add(clmlMap);
        }

        return reusltMapList;
    }

    /** 上一次检查报告列表 */
    public HttpResult selectSycjcbgList(String sqsxid){
        Map map = new HashMap();

        map.put("sycfhxbgList", this.getSycjcbgLists(sqsxid, null));

        return HttpResult.ok(map);
    }

    /**
     * 上一次检查报告列表
     */
    private List<SpcySqsxWjjl> getSycjcbgLists(String sqsxid, List<SpcySqsxWjjl> wjjlList){
        List<SpcySqsxWjjl> sycfhxbgList = new ArrayList<>(); // 上一次符合性检查报告
        try {
            List<String> sycSqQyList = spcySqsxBaiscService.selectSycSqQyById(sqsxid);
            if (CollectionUtil.isNotEmpty(sycSqQyList)){
                sycfhxbgList = spcySqsxWjjlService.list(new LambdaQueryWrapper<SpcySqsxWjjl>()
                        .eq(SpcySqsxWjjl::getFpid, sycSqQyList.get(0))
                        .eq(SpcySqsxWjjl::getFwjlx1, WjmlConstant.xcjcbg));

                for (SpcySqsxWjjl oldbg : sycfhxbgList){
                    if (StringUtils.isBlank(oldbg.getFwjnr()) || !oldbg.getFwjnr().contains("上一次报告")) {
                        oldbg.setFwjnr(oldbg.getFwjnr() + "@上一次报告");
                        spcySqsxWjjlService.updateById(oldbg);
                    }
                }
            }

            if (CollectionUtil.isEmpty(sycfhxbgList)){
                if (CollectionUtil.isEmpty(wjjlList)) {
                    sycfhxbgList = spcySqsxWjjlService.list(new LambdaQueryWrapper<SpcySqsxWjjl>()
                            .eq(SpcySqsxWjjl::getFpid, sqsxid)
                            .eq(SpcySqsxWjjl::getFwjlx1, WjmlConstant.sycfhxjcbg)
                            .orderByAsc(SpcySqsxWjjl::getCreateTime));
                }else {
                    sycfhxbgList = wjjlList.stream().filter(f -> StringUtils.isNotBlank(f.getFwjlx1())
                            && f.getFwjlx1().equals(WjmlConstant.sycfhxjcbg)).collect(Collectors.toList());
                }
            }else {
                List<SpcySqsxWjjl> sycfhxbgList1 = null;
                if (CollectionUtil.isEmpty(wjjlList)) {
                    sycfhxbgList1 = spcySqsxWjjlService.list(new LambdaQueryWrapper<SpcySqsxWjjl>()
                            .eq(SpcySqsxWjjl::getFpid, sqsxid)
                            .eq(SpcySqsxWjjl::getFwjlx1, WjmlConstant.sycfhxjcbg)
                            .orderByAsc(SpcySqsxWjjl::getCreateTime));
                }else {
                    sycfhxbgList1 = wjjlList.stream().filter(f -> StringUtils.isNotBlank(f.getFwjlx1())
                            && f.getFwjlx1().equals(WjmlConstant.sycfhxjcbg)).collect(Collectors.toList());
                }

                if (CollectionUtil.isNotEmpty(sycfhxbgList1)){
                    for (SpcySqsxWjjl bean : sycfhxbgList1){
                        sycfhxbgList.add(bean);
                    }
                }
            }
        }catch (Exception e){
            log.error("上一次符合性检查报告-错误：{}", e.getMessage());
        }

        return sycfhxbgList;
    }

    /**
     * 获取事项文件列表数据 - 方案制定环节
     * @param sqsxid 申请事项id
     */
    public Map selectFazdWjList(String sqsxid){
        Map map = new HashMap();

        // 获取申请事项-相关文件(不属于行政审评的数据)
        /*LambdaQueryWrapper<SpcySqsxWjjl> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SpcySqsxWjjl::getFpid,sqsxid);
        queryWrapper.orderByAsc(SpcySqsxWjjl::getCreateTime);
        List<SpcySqsxWjjl> wjjlList = spcySqsxWjjlService.list(queryWrapper);*/
        List<String> typeList = CollectionUtil.newArrayList(
                WjmlConstant.xpxcscydh,
                WjmlConstant.xpxcgcydh,
                WjmlConstant.qydwjctz,
                WjmlConstant.jcfa,
                WjmlConstant.jczgclyq,
                WjmlConstant.sycfhxjcbg
        );
        List<SpcySqsxWjjl> wjjlList = spcySqsxWjjlService.findWjjlListByFwjlx1(typeList, sqsxid);

        String ywflCode = spcySqsxBaiscService.getYwflCodeBySqsxId(sqsxid); // 业务分类编号

        // 方案环节
        List<SpcySqsxWjjl> xpxccydhList = new ArrayList<>(); // 选派现场审查员的函
        List<SpcySqsxWjjl> xpxcgcydhList = new ArrayList<>(); // 选派现场观察员的函
        List<SpcySqsxWjjl> qydwjctzList = new ArrayList<>(); // 企业/单位检查通知
        List<SpcySqsxWjjl> jcfaList = new ArrayList<>(); // 检查方案

        xpxccydhList = wjjlList.stream().filter(f -> StringUtils.isNotBlank(f.getFwjlx1())
                && f.getFwjlx1().equals(WjmlConstant.xpxcscydh)).collect(Collectors.toList());
        map.put("xpxccydhList",xpxccydhList); // 选派现场审查员的函

        if (StringUtils.isNotBlank(ywflCode) && ywflCode.equals(SpcyConstant.YWFL_CODE_YPJY)) {
//            map.put("xpxcgcydhList", xpxcgcydhList); // 选派现场观察员的函


            /*List<SpcySqsxWjjl> xzjchList = new ArrayList<>(); // 协助检查函 2023-0726
            xzjchList = wjjlList.stream().filter(f -> StringUtils.isNotBlank(f.getFwjlx1())
                    && f.getFwjlx1().equals(WjmlConstant.xzjch)).collect(Collectors.toList());
            map.put("xzjchList",xzjchList); // 协助检查函*/

            /*List<SpcySqsxWjjl> jsscbgList = new ArrayList<>(); // 技术审查报告
            jsscbgList = wjjlList.stream().filter(f -> StringUtils.isNotBlank(f.getFwjlx1())
                    && f.getFwjlx1().equals(WjmlConstant.jsscbg)).collect(Collectors.toList());
            map.put("jsscbgList",jsscbgList);*/

        }
        else if (StringUtils.isNotBlank(ywflCode) && ywflCode.equals(SpcyConstant.YWFL_CODE_HZP)){

            /*xpxcgcydhList = wjjlList.stream().filter(f -> StringUtils.isNotBlank(f.getFwjlx1())
                    && f.getFwjlx1().equals(WjmlConstant.xpxcgcydh)).collect(Collectors.toList());
            map.put("xpxcgcydhList", xpxcgcydhList); // 选派现场观察员的函*/

        }else if (StringUtils.isNotBlank(ywflCode) && (ywflCode.equals(SpcyConstant.YWFL_CODE_YPZC) || ywflCode.equals(SpcyConstant.YWFL_CODE_YPZCLC))){

            /*xpxcgcydhList = wjjlList.stream().filter(f -> StringUtils.isNotBlank(f.getFwjlx1())
                    && f.getFwjlx1().equals(WjmlConstant.xpxcgcydh)).collect(Collectors.toList());
            map.put("xpxcgcydhList", xpxcgcydhList); // 选派现场观察员的函*/

           /* List<SpcySqsxWjjl> cydList = new ArrayList<>(); // 抽样单
            cydList = wjjlList.stream().filter(f -> StringUtils.isNotBlank(f.getFwjlx1())
                    && f.getFwjlx1().equals(WjmlConstant.ypzc_cyd)).collect(Collectors.toList());
            map.put("cydList",cydList);*/

            /*List<SpcySqsxWjjl> cyfqList = new ArrayList<>(); // 抽样封签
            cyfqList = wjjlList.stream().filter(f -> StringUtils.isNotBlank(f.getFwjlx1())
                    && f.getFwjlx1().equals(WjmlConstant.ypzc_cyfq)).collect(Collectors.toList());
            map.put("cyfqList",cyfqList);*/

        }
        else {
            /*xpxcgcydhList = wjjlList.stream().filter(f -> StringUtils.isNotBlank(f.getFwjlx1())
                    && f.getFwjlx1().equals(WjmlConstant.xpxcgcydh)).collect(Collectors.toList());
            map.put("xpxcgcydhList", xpxcgcydhList); // 选派现场观察员的函*/
        }

        xpxcgcydhList = wjjlList.stream().filter(f -> StringUtils.isNotBlank(f.getFwjlx1())
                && f.getFwjlx1().equals(WjmlConstant.xpxcgcydh)).collect(Collectors.toList());
        map.put("xpxcgcydhList", xpxcgcydhList); // 选派现场观察员的函

        qydwjctzList = wjjlList.stream().filter(f -> StringUtils.isNotBlank(f.getFwjlx1())
                && f.getFwjlx1().equals(WjmlConstant.qydwjctz)).collect(Collectors.toList());
        map.put("qydwjctzList",qydwjctzList); // 企业单位检查通知

        jcfaList = wjjlList.stream().filter(f -> StringUtils.isNotBlank(f.getFwjlx1())
                && f.getFwjlx1().equals(WjmlConstant.jcfa)).collect(Collectors.toList());
        map.put("jcfaList",jcfaList); // 检查方案

        List<SpcySqsxWjjl> jczgclyqList = new ArrayList<>(); // 检查整改材料要求
        jczgclyqList = wjjlList.stream().filter(f -> StringUtils.isNotBlank(f.getFwjlx1())
                && f.getFwjlx1().equals(WjmlConstant.jczgclyq)).collect(Collectors.toList());
        map.put("jczgclyqList",jczgclyqList);

        map.put("sycfhxbgList", this.getSycjcbgLists(sqsxid, wjjlList)); // 上一次符合性检查报告

        /*List<SpcySqsxWjjl> bztzList = new ArrayList<>();
        bztzList = wjjlList.stream().filter(f -> StringUtils.isNotBlank(f.getFwjlx1())
                && f.getFwjlx1().equals(WjmlConstant.bztz_YLqx)).collect(Collectors.toList());
        map.put("bztzList",bztzList); // 补正通知*/

        /*List<SpcySqsxWjjl> bczgtzList = new ArrayList<>();
        bczgtzList = wjjlList.stream().filter(f -> StringUtils.isNotBlank(f.getFwjlx1())
                && f.getFwjlx1().equals(WjmlConstant.bztz)).collect(Collectors.toList());
        map.put("bczgtzList",bczgtzList); // 补充整改通知*/

        return map;
    }
}
